import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:grint/constant/colors.dart';
import 'package:grint/constant/icon_font.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import '../../enum/app_routes.dart';
import '../upload/create_page.dart';

class RootLayout extends StatelessWidget {
  final Widget child;
  final String location;

  const RootLayout({super.key, required this.child, required this.location});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: child,
      extendBody: true,
      bottomNavigationBar: CustomBottomBar(location: location),
    );
  }
}

class CustomBottomBar extends StatefulWidget {
  final String location;

  const CustomBottomBar({super.key, required this.location});

  @override
  State<CustomBottomBar> createState() => _CustomBottomBarState();
}

class _CustomBottomBarState extends State<CustomBottomBar> {
  late int _selectedIndex;

  // 底部导航图标列表
  final List<IconData> _icons = [
    IconFontIcons.iconYachi,
    IconFontIcons.iconXingqiu,
    IconFontIcons.iconShejiao,
    IconFontIcons.iconProfile,
  ];

  @override
  void initState() {
    super.initState();
    _updateSelectedIndex();
  }

  @override
  void didUpdateWidget(CustomBottomBar oldWidget) {
    super.didUpdateWidget(oldWidget);
    if (oldWidget.location != widget.location) {
      _updateSelectedIndex();
    }
  }

  void _updateSelectedIndex() {
    if (widget.location.startsWith(AppRoutes.home)) {
      _selectedIndex = 0;
    } else if (widget.location.startsWith(AppRoutes.discovery)) {
      _selectedIndex = 1;
    } else if (widget.location.startsWith(AppRoutes.message)) {
      _selectedIndex = 2;
    } else if (widget.location.startsWith(AppRoutes.profile)) {
      _selectedIndex = 3;
    } else {
      _selectedIndex = 0;
    }
  }

  void _onItemTapped(int index) {
    if (_selectedIndex == index) return;

    setState(() {
      _selectedIndex = index;
    });

    switch (index) {
      case 0:
        context.go(AppRoutes.home);
        break;
      case 1:
        context.go(AppRoutes.discovery);
        break;
      case 2:
        context.go(AppRoutes.message);
        break;
      case 3:
        context.go(AppRoutes.profile);
        break;
    }
  }

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: EdgeInsets.only(bottom: 24.h, left: 16.w, right: 16.w),
      child: Container(
        height: 68.h,
        decoration: BoxDecoration(
          color: AppColors.lightBlue,
          borderRadius: BorderRadius.circular(36.r),
        ),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: [
            _buildNavItem(0),
            _buildNavItem(1),

            GestureDetector(
              onTap: () {
                context.push(
                  '/create',
                  extra: {'uploadType': ContentUploadType.dailyShare},
                );
              },
              child: Container(
                width: 48.r,
                height: 48.r,
                decoration: BoxDecoration(
                  color: Colors.transparent,
                  shape: BoxShape.circle,
                  border: Border.all(color: AppColors.blueSecond, width: 2.r),
                ),
                child: Icon(
                  Icons.add,
                  color: AppColors.blueSecond,
                  size: 32.sp,
                ),
              ),
            ),

            _buildNavItem(2),
            _buildNavItem(3),
          ],
        ),
      ),
    );
  }

  Widget _buildNavItem(int index) {
    final bool isSelected = _selectedIndex == index;

    return GestureDetector(
      onTap: () => _onItemTapped(index),
      child: Container(
        width: 48.r,
        height: 48.r,
        decoration:
            isSelected
                ? BoxDecoration(color: AppColors.blue, shape: BoxShape.circle)
                : null,
        child: Center(
          child: Icon(_icons[index], color: AppColors.icon, size: 26.sp),
        ),
      ),
    );
  }
}
